#!/usr/bin/env python
#
#       Property.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import wx

import caselogic
import handleargs
import accessories
import generatedefinitions
import pytestgenerator

class Displayselected:
    def __init__(self,baseframe) :
        """
        BaseFrame Instanstiation
        Display the property of a selected entity
        Name, Type ,Args, ArgType of the selected Entity
        """
        self.Inst=baseframe

    def gatherinfo(self,event):
        """
        Gather Required Info
        """
        self.analyze(self.retrive())

    def retrive(self):

        selected=[]
        selected.append(str(self.Inst.modname.GetValue()))
        selected_entity=self.Inst.avail.FindItem(0,\
            self.Inst.avail.GetItemText(int(self.Inst.avail.GetItem\
            (self.Inst.avail.GetFirstSelected()).GetText())-1))
        entity_type=self.Inst.avail.GetItem(selected_entity,3).GetText()

        if entity_type=='METHOD':
            selected.append(str\
                (self.Inst.avail.GetItem(selected_entity,2).GetText()))
        else:
            selected.append(str\
                (self.Inst.avail.GetItem(selected_entity,1).GetText()))

        selected.append(str(entity_type))
        if entity_type=='METHOD':
            selected.append(str(generatedefinitions.GenerateEvents\
                (self.Inst)._findclass(selected_entity)))

        return selected

    def analyze(self,data):
        """
        Collect information for the selected entity
        """
        temp=[]
        genargs=[]
        deftemp=[]
        temp.extend(data) #for local manipulations

        try :
            module=__import__(temp.pop(0))
        except Exception, E:
            module=None
            accessories.infobox(self.Inst,\
                "Import Error:\nPlease enter module name in the entry box",\
                "Import Error",wx.OK | wx.ICON_EXCLAMATION)

        entity=str(temp.pop(0))
        if str(data[2]) == "CLASS" :
            self.post([entity,str(data[2]),module.__name__,entity,\
                "Refer to __init__ method","--do--","--do--"])
            return 0
        elif module and data[2] == "METHOD":
            modattr=getattr(module,temp[1])
            entityattr=getattr(modattr,entity)
            mclass=modattr.__name__
            clargs=pytestgenerator.TestGenerator()._classargs(modattr,module)
        elif module:
            modattr=module
            entityattr=getattr(modattr,entity)
            mclass=None
            clargs=None

        arginfo=handleargs.Args().describeArgs(entityattr)
        if arginfo and len(arginfo[0]) > 0:
            if arginfo[0][0] == "self" :
                arginfo[0].pop(0)
        temp=caselogic.ArgLogic()._prediction\
            (entity,Args=arginfo,mod=modattr,classmod=mclass,clargs=clargs)

        if temp[0] and arginfo[0]:
            self.post([entity,str(data[2]),module.__name__,mclass,\
                ",".join(arginfo[0]),\
                (map(lambda arg : type(arg),temp[2])),temp[1]])
        elif arginfo[0]:
            self.post([entity,str(data[2]),module.__name__,\
                mclass,",".join(arginfo[0]),None,None])
        else:
            self.post([entity,str(data[2]),\
                module.__name__,mclass,None,None,None])

    def post(self,pdat):
        """
        Properties Listed
        Name:
        Category:
        Module:
        Class:
        Arguments:
        Predicted Arguments:
        Default Arguments:
        """
        accessories.infobox(self.Inst,\
            "\tNAME : %s\n\n\
            CATEGORY : %s\n\n\
            MODULE : %s\n\n\
            CLASS : %s\n\n\
            ARGUMENTS : %s\n\n\
            PREDICTED ARG TYPES : %s\n\n\
            DEFAULT ARGS : %s\n\n"
            %(pdat[0],pdat[1].lower(),pdat[2],pdat[3],pdat[4],pdat[5],pdat[6]),\
            "PyTestsGen Property",wx.OK)
